package cn.harry.cabinet.view

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import cn.harry.cabinet.R
import cn.harry.cabinet.model.entity.User
import cn.harry.cabinet.theme.manager.ThemeManager
import cn.harry.cabinet.utils.VoiceHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 验证页面基类
 * 提取三个验证页面（密码、刷卡、人脸）的公共逻辑
 */
abstract class BaseAuthActivity : AppCompatActivity() {
    
    companion object {
        const val EXTRA_ACTION_TYPE = "action_type"
        const val ACTION_RETURN = "return"
        const val ACTION_TAKE = "take"
    }
    
    protected var actionType: String = ACTION_TAKE
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 获取操作类型
        actionType = intent.getStringExtra(EXTRA_ACTION_TYPE) ?: ACTION_TAKE
    }
    
    /**
     * 验证成功后的处理
     * 跳转到对应的仪器操作页面
     */
    protected open fun onAuthSuccess(user: User, authMethod: String) {
        // 播放欢迎语音
        VoiceHelper.speakWelcome(user.name)
        
        // 跳转到对应的仪器操作页面
        val intent = when (actionType) {
            ACTION_RETURN -> Intent(this, ReturnInstrumentActivity::class.java)
            else -> Intent(this, TakeInstrumentActivity::class.java)
        }
        
        intent.putExtra("user_id", user.id)
        intent.putExtra("user_name", user.name)
        intent.putExtra("employee_id", user.employeeId)
        intent.putExtra("auth_method", authMethod)
        
        startActivity(intent)
        finish()
    }
    
    /**
     * 验证失败的处理
     */
    protected fun onAuthFailed(reason: String) {
        VoiceHelper.speakAuthFailed(reason)
        Toast.makeText(this, reason, Toast.LENGTH_SHORT).show()
    }
    
    /**
     * 应用主题
     * 从数据库加载主题并应用到当前Activity
     */
    protected fun applyTheme() {
        lifecycleScope.launch {
            try {
                val theme = withContext(Dispatchers.IO) {
                    ThemeManager.loadThemeFromDatabase(this@BaseAuthActivity)
                }
                withContext(Dispatchers.Main) {
                    ThemeManager.applyTheme(this@BaseAuthActivity, theme)
                    // 根据主题设置背景
                    val backgroundRes = when (theme) {
                        ThemeManager.Theme.CHINA_RED -> R.drawable.china_red_background
                        ThemeManager.Theme.VIBRANT_ORANGE -> R.drawable.vibrant_orange_background
                        ThemeManager.Theme.TECH_BLUE -> R.drawable.tech_blue_background
                    }
                    window.decorView.setBackgroundResource(backgroundRes)
                }
            } catch (e: Exception) {
                val defaultTheme = ThemeManager.getCurrentTheme(this@BaseAuthActivity)
                ThemeManager.applyTheme(this@BaseAuthActivity, defaultTheme)
                // 根据主题设置背景
                val backgroundRes = when (defaultTheme) {
                    ThemeManager.Theme.CHINA_RED -> R.drawable.china_red_background
                    ThemeManager.Theme.VIBRANT_ORANGE -> R.drawable.vibrant_orange_background
                    ThemeManager.Theme.TECH_BLUE -> R.drawable.tech_blue_background
                }
                window.decorView.setBackgroundResource(backgroundRes)
            }
        }
    }
    
    /**
     * 获取页面标题
     * 根据操作类型返回对应的标题
     */
    protected fun getPageTitle(authType: String): String {
        val action = when (actionType) {
            ACTION_RETURN -> "还仪器"
            else -> "取仪器"
        }
        return "$action - $authType"
    }
}
